<template>
  <el-container class="container">
    <!-- 头部区域开始 -->
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt />
        <span>后台电商管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 头部区域结束 -->

    <el-container>
      <!-- 侧边栏区域开始 -->
      <el-aside :width= "iscallapse ? '64px': '200px'">
        <!-- 点击控制侧边栏是否折叠 -->
        <div class="toggle" @click="toggle">|||</div>
        <!-- 一级导航 -->
        <el-menu background-color=" #333944" text-color="#fff" active-text-color="skyblue" :unique-opened="true" :collapse='iscallapse' :router="true" :default-active='activePath'>
          <!-- :index后面的值只能接受字符串给index动态添加不同的值以免点击的时候下面的列表一起显示 -->
          <el-submenu :index="item.id.toString()" v-for="item in menuData" :key='item.id'>
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconList[item.id]"></i>
              <!-- 文本 -->
              <span>{{item.authName}}</span>
            </template>
           <!-- 二级导航 -->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key='subItem.id' @click="saveStatus('/'+subItem.path)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 侧边栏结束 -->

      <!-- 主体内容区域开始 -->
      <el-main>
        <router-view></router-view>
      </el-main>
      <!-- 主体内容区域结束 -->
    </el-container>
  </el-container>
</template>


<script>
export default {
  data() {
    return {
      menuData: [],
      iscallapse:false,
      activePath:'',
      iconList:{
        '125':'iconfont icon-user',
        '103':'iconfont icon-tijikongjian',
        '101':'iconfont icon-shangpin',
        '102':'iconfont icon-danju',
        '145':'iconfont icon-baobiao',
      }
    }
  },
  //在页面刚加载的时候就立即发送请求
  created() {
    //调用发送请求的函数
    this.getData()
    //获取session中的参数
    this.activePath=window.sessionStorage.getItem('ps')
  },
  methods: {
    //切换侧边栏收放
    toggle(){
      this.iscallapse=!this.iscallapse
    },
    //保存连接激活状态
    saveStatus(pathStatus){
    window.sessionStorage.setItem('ps',pathStatus)
    this.activePath=pathStatus
    },
    //发送请求异步函数
    async getData() {
      const { data: res } = await this.$http.get('/menus')
      //console.log(res)对象里面有五个数组
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      //将后天返回数据添加到data（）中的menuData中
      this.menuData = res.data
     // console.log(this.menuData)
    },
    logout() {
      //实现退出功能直接将token值清空
      // window.sessionStorage.clear()
      //清楚当前的token
      window.sessionStorage.removeItem('token')
      //退出后跳转到登录页面
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding: 0;
  align-items: center;
  .logo {
    display: flex;
    align-items: center;
    span {
      margin-left: 20px;
      color: #fff;
      font-size: 20px;
    }
  }
}
.el-aside {
  background-color: #333944;
  .el-menu {
    border: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 15px;
}
.toggle{
  background-color: #ccc;
  background-color: #333944;
  text-align: center;
  line-height: 20px;
}
</style>